# coding:utf8
'''
    页面配置相关
'''
from flask_restful import request, Resource
from _sys.models import MongodbOperations, Token, ResponseJson, CreateId, FormatIO
from _sys.parser import parsers
from _sys.function import had_power, md5Encode, parse_param_info, allow_cross, format_words
from _sys.code import dbNameDict

mgOp = MongodbOperations()
token = Token()
responseJson = ResponseJson()

class PageManage(Resource):

    '''
        前端页面元素控制
    '''
    @allow_cross
    def get(self):

        args = parsers()
        pg = args.get('pg')  # 页码
        ele = args.get('ele')  # 元素
        try:
            rd = mgOp.find_many(dbNameDict['adminPageSettingData'], filter={'pg': pg, 'ele': ele})
            return responseJson.correct(code='ok', data=rd)
        except Exception:
            return responseJson.error_response(code='unknownError')

    @allow_cross
    def post(self):
        args = parsers()
        pg = args.get('pg')  # 页码
        ele = args.get('ele')  # 元素
        a_id = args.get('a_id')
        a_token = args.get('a_token')

        if not token.verify(a_token, a_id):
            return responseJson.error_response(code='permissionsValidationFailure')

        if pg == 'index':  # 首页
            if ele == 'tpb':
                cn = args.get('cn')  # 简体文字  lin1|lin2|lin3
                tw = args.get('tw')  # 繁体文字
                en = args.get('en')  # 英文
                video = args.get('video')  # 视频链接
                img = args.get('img')  # 图片链接
                index = args.get('index')  # 轮播索引
                has_video = args.get('has_video')

                n_d = {
                    'cn': cn.split('|'),
                    'tw': tw.split('|'),
                    'en': en.split('|'),
                    'video': video,
                    'img': img,
                    'has_video':has_video
                }
                rd = mgOp.find_one_and_update(dbNameDict['adminPageSettingData'],
                                              filter={'pg': pg, 'index': index, 'ele': ele}, new_data=n_d)
                return responseJson.correct(code='ok', data=rd)

        if pg == 'agent':  # 代理页面
            if ele == 'tpb':
                cn = args.get('cn')  # 简体文字  lin1|lin2|lin3
                tw = args.get('tw')  # 繁体文字
                en = args.get('en')  # 英文
                img = args.get('img')  # 图片链接
                index = args.get('index')  # 轮播索引

                n_d = {
                    'cn': cn.split('|'),
                    'tw': tw.split('|'),
                    'en': en.split('|'),
                    'img': img,
                }
                rd = mgOp.find_one_and_update(dbNameDict['adminPageSettingData'],
                                              filter={'pg': pg, 'index': index, 'ele': ele}, new_data=n_d)
                return responseJson.correct(code='ok', data=rd)

        return responseJson.error_response(code='permissionsValidationFailure')

    @allow_cross
    def put(self):
        '''
            新增页面元素
        :return:
        '''
        args = parsers()
        pg = args.get('pg')  # 页码
        ele = args.get('ele') # 元素
        a_id = args.get('a_id')
        a_token = args.get('a_token')

        if not token.verify(a_token, a_id):
            return responseJson.error_response(code='permissionsValidationFailure')

        a_role_id = mgOp.find_one(dbNameDict['adminData'], filter={'a_id': a_id})['a_role_id']
        powers = mgOp.find_one(dbNameDict['adminRoleData'], filter={'role_id': a_role_id})['powers']

        #if not had_power('CanAddProductSN', powers):  # 不具权限
            #return responseJson.error_response(code='permissionsValidationFailure')

        if pg == 'index' and ele == 'tpb':   # 首页
            cn = args.get('cn')  # 简体文字  lin1|lin2|lin3
            tw = args.get('tw')  # 繁体文字
            en = args.get('en')  # 英文
            video = args.get('video')  # 视频链接
            img = args.get('img') # 图片链接
            index = args.get('index')  # 轮播索引
            has_video = args.get('has_video')

            n_d = {
                'cn': cn.split('|'),
                'tw': tw.split('|'),
                'en': en.split('|'),
                'pg': pg,
                'ele': ele,
                'video': video,
                'index': index,
                'has_video':has_video,
                'img': img
            }
            rd = mgOp.insert_one(dbNameDict['adminPageSettingData'], n_d)
            return responseJson.correct(code='ok', data=rd)

        if pg == 'agent' and ele == 'tpb':
            cn = args.get('cn')  # 简体文字  lin1|lin2|lin3
            tw = args.get('tw')  # 繁体文字
            en = args.get('en')  # 英文
            img = args.get('img')  # 图片链接
            index = args.get('index')  # 轮播索引

            n_d = {
                'cn': cn.split('|'),
                'tw': tw.split('|'),
                'en': en.split('|'),
                'pg': pg,
                'ele': ele,
                'index': index,
                'img': img
            }
            rd = mgOp.insert_one(dbNameDict['adminPageSettingData'], n_d)
            return responseJson.correct(code='ok', data=rd)

    @allow_cross
    def delete(self):

        args = parsers()
        pg = args.get('pg')  # 页码
        ele = args.get('ele')  # 元素
        a_id = args.get('a_id')
        a_token = args.get('a_token')

        if not token.verify(a_token, a_id):
            return responseJson.error_response(code='permissionsValidationFailure')

        a_role_id = mgOp.find_one(dbNameDict['adminData'], filter={'a_id': a_id})['a_role_id']
        powers = mgOp.find_one(dbNameDict['adminRoleData'], filter={'role_id': a_role_id})['powers']

        # if not had_power('CanAddProductSN', powers):  # 不具权限
        # return responseJson.error_response(code='permissionsValidationFailure')

        if pg == 'index' and ele == 'tpb':   # 顶层轮播
            index = args.get('index')  # 轮播索引
            rd = mgOp.delete_one(dbNameDict['adminPageSettingData'], filter={'pg': pg, 'index': index, 'ele': ele})
            return responseJson.correct(code='ok', data=rd)

        if pg == 'agent' and ele == 'tpb':
            index = args.get('index')  # 轮播索引
            rd = mgOp.delete_one(dbNameDict['adminPageSettingData'], filter={'pg': pg, 'index': index, 'ele': ele})
            return responseJson.correct(code='ok', data=rd)




























